const request = require('request');
const config  = require('../config/config');

class helium {
    constructor() {
    }

    async valid(cookie){
        return new Promise((resolve, reject) => {
            let options = {
                url: 'https://members.helium10.com/api/v1/customers/accounts',
                method: 'GET',
                // proxy: `http://${config.PROXY.HOST}:${config.PROXY.PORT}`,
                headers: {
                    "cookie": cookie,
                    // "accept": 'application/json, text/javascript, */*; q=0.01'
                }
            };

            request(options, async (err, response, body) => {
                if (err) {
                    console.log(`helium10 cookie invalid. error:${JSON.stringify(err)}`);
                    resolve(false);
                } else {
                    body = JSON.parse(body);
                    if(body.status === 401 && !body.results){
                        console.log(`helium10 cookie invalid. Response statusCode:${body.status}`)
                        resolve(false);
                    }else{
                        console.log(`helium10 cookie valid. Results:${JSON.stringify(body.results)}`)
                        resolve(true)
                    }
                }
            })
        })
    }
}

module.exports = helium;
